//
//  MineSecondTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/10.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "MineSecondTableViewCell.h"
#import "MineSecondCollectionViewCell.h"

#import "PurchaserOrderViewController.h"
#import "BuyOrderViewController.h"

#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import "JDKeplerManager.h"
#import "TbOrderWebViewController.h"
#import "ATBReFundListViewController.h"
static NSString *cellID = @"cellID";

@interface MineSecondTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation MineSecondTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    
        [self.contentView addSubview:self.collectionView];
    }
    
    return self;
    
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(86));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(86));
        make.top.mas_equalTo(weakSelf.contentView.mas_top);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        
    }];
}

#pragma mark -delegate\datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arr.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MineSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.dict = self.arr[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self.parentVC]) {
        
        return;
    }
    
//    NSDictionary *dict = self.arr[indexPath.item];
    
    if (self.cellType == 1) {
        
        NSLog(@"第二组cell点击");
        
        // 进货订单
        PurchaserOrderViewController *orderVC = [[PurchaserOrderViewController alloc] init];
        orderVC.type = indexPath.item;
        [self.parentVC.navigationController pushViewController:orderVC animated:YES];
   
    }else {
        
        
//        if ([[dict allKeys] containsObject:@"status"]) {
//
//            NSLog(@"弹提示框");
//            
//        }else {
        
            // 购买订单
            NSLog(@"第三组cell点击");
            
            if (indexPath.item == 0) {
                // 我的订单
                BuyOrderViewController *buyOrderVC = [[BuyOrderViewController alloc] init];
                [self.parentVC.navigationController pushViewController:buyOrderVC animated:YES];
                
            }else if(indexPath.item == 1) {
                
                TbOrderWebViewController *vc = [[TbOrderWebViewController alloc] init];
                vc.title = @"淘宝订单";
                id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
                AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
                showParam.openType = AlibcOpenTypeH5;
                showParam.backUrl = @"tbopen25027070";
                
                // 优先拉起手淘：@"taobao_scheme" \\\ 天猫：tmall_scheme
                showParam.linkKey = @"taobao_scheme";
                showParam.isNeedPush = YES;
                showParam.nativeFailMode = AlibcNativeFailModeJumpH5;
                
                NSInteger res = [[AlibcTradeSDK sharedInstance].tradeService show:vc webView:vc.webView page:page showParams:nil taoKeParams:nil trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
                    
                } tradeProcessFailedCallback:^(NSError * _Nullable error) {
                    
                }];
                
                if (res) {
                    
                    [self.parentVC.navigationController pushViewController:vc animated:YES];
                }
                
            } else if (indexPath.item == 2) {
                [[JDKeplerManager sharedManager] jumpToTheOrderListPageController:self.parentVC];
            } else if (indexPath.item == 3) {
                ATBReFundListViewController *vc = [[ATBReFundListViewController alloc]init];
                vc.title = @"退款订单";
//                vc.tradeType = @"7";
                [self.parentVC.navigationController pushViewController:vc animated:YES];
                NSLog(@"进来了");
            }
            
        }
        
//    }

    
}

#pragma mark -lazy
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(80), jisuan(106));
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = (WIDTH - jisuan(86) * 2 - jisuan(80) * 4) / 3.0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = WhiteColor;
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[MineSecondCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        
        
        if (@available(iOS 11.0 , *)) {
            
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _collectionView;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
